﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BEPUphysics.Collidables;
using BEPUphysics.DataStructures;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using BEPUphysics.MathExtensions;

namespace The_LarX
{
    public class BepuEntity_TriangleMesh
    {
        public StaticMesh Mesh;

        private string modelName;
        public string ModelName
        {
            get { return modelName; }
            set
            {
                modelName = value;
                Model model = Engine.Content.Load<Model>(value);
                Vector3[] staticTriangleVertices;
                int[] staticTriangleIndices;

                TriangleMesh.GetVerticesAndIndicesFromModel(model,
                    out staticTriangleVertices, out staticTriangleIndices);
                if (Mesh != null)
                {
                    BepuPhysics.ActualBepuPhysics.Space.Remove(Mesh);
                }
                Mesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices,
                    new AffineTransform(Vector3.Zero));

                BepuPhysics.ActualBepuPhysics.Space.Add(Mesh);
            }
        }

        public BepuEntity_TriangleMesh()
        {
        }
        public BepuEntity_TriangleMesh(string ModelName)
        {
            this.ModelName = ModelName;
        }

        public void SerializeData(SerializeData Data)
        {
            Data.AddData("TriangleMesh.ModelName", this.ModelName);
        }
        public void DeserializeData(SerializeData Data)
        {
            this.ModelName = Data.GetData<string>("TriangleMesh.ModelName");
        }
    }
}
